function cr = slcorrectrate(scores, rlabels, qlabels, sch, varargin)
%SLCORRECTRATE Computes the correct rate of classification
%
% [ Syntax ]
%   - cr = slcorrectrate(scores, rlabels, qlabels, sch) 
%
% [ Arguments ]
%   - scores:           the scores matrix (nr x nq)
%   - clabels:          the vector of labels of the reference samples
%   - qlabels:          the vector of groundtruth labels of query samples
%   - sch:              the classification scheme
%   - cr:               the correct rate of the score-based classification
%
% [ Description ]
%   - cr = slcorrectrate(scores, clabels, slabels, sch, ...) 
%     computes the classification correct rate. 
%
%     Suppose we want to classify n samples into m classes, then scores 
%     will be an m x n matrix, with the entry at i-th row, j-th column 
%     representing the score of the j-th sample in the i-th class. 
%     
%     The classification are done following the scheme specified by sch.
%     You can further specify options to control the classification.
%     This is accomplished by invoking slclassify. Please refer to
%     the help of classify for detailed information about sch and options.
% 
% [ History ]
%   - Created by Dahua Lin on Jun 10th, 2005
%   - Modified by Dahua Lin on May 1st, 2005
%     - To base on the sltoolbox v4.
%   - Modified by Dahua Lin on Aug 9th, 2006
%     - Extract slclassify as independent function and base on it
%   - Modified by Dahua Lin on Aug 16th, 2006
%     - Based on new slclassify to support multiple schemes
%   - Modified by Dahua Lin on Jul 19, 2007
%     - Based on new slclassify to support a series of new schemes.
%

%% parse and verify input arguments

error(nargchk(4, inf, nargin));

assert(isnumeric(scores) && ndims(scores) == 2, 'sltoolbox:slcorrectrate:invalidarg', ...
    'The scores should be a 2D numeric matrix.');
assert(isnumeric(rlabels) && isvector(rlabels), 'sltoolbox:slcorrectrate:invalidarg', ...
    'The rlabels should be a numeric vector.');
assert(isnumeric(qlabels) && isvector(qlabels), 'sltoolbox:slcorrectrate:invalidarg', ...
    'The qlabels should be a numeric vector.');
assert(size(scores, 1) == length(rlabels) && size(scores, 2) == length(qlabels), ...
    'sltoolbox:slcorrectrate:sizmismatch', 'The size of scores and that of the labels are not matched.');

assert(ischar(sch), 'sltoolbox:slcorrectrate:invalidarg', ...
    'sch should be a string indicating the name of the classification scheme.');

%% Make decision

decisions = slclassify(scores, rlabels, sch, varargin{:});

%% Evaluate correct rate

if ~isequal(size(decisions), size(qlabels))
    decisions = reshape(decisions, size(qlabels));
end
cr = sum(decisions == qlabels) / numel(decisions);







